package com.dream.prm.common;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Timer;
import java.util.TimerTask;

import javax.annotation.Resource;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.socket.TextMessage;

import com.dream.prm.socket.MyWebSocketHandler;
import com.sun.org.glassfish.gmbal.DescriptorFields;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * @author w.g
 * @date 2019-07-18
 * 
 */
@Component("springInit")
@SuppressWarnings("all")
public class SpringInit extends HttpServlet{
	
	public static Properties prop;
	public MyWebSocketHandler myWebSocketHandler;
	
	@Override
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
		super.init(config);
		ServletContext servletContext = config.getServletContext();
		WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
		myWebSocketHandler=(MyWebSocketHandler) webApplicationContext.getBean("myWebSocketHandler");
		readProperties(config);
	}
	
	/**
	 * 读配置文件
	 */
	public void readProperties(ServletConfig config) {
		//编译后放置于class目录下
		String path = SpringInit.class.getResource("/conf/settings.properties").getPath(); 
		prop=PropertiesUtil(path);
		if(prop!=null) {
			sendSocketMsg();
		}
	}
	
	
	/**
	 * TODO 读取配置文件
	 * @param path 文件路径
	 */
	public Properties PropertiesUtil(String path) {
		//System.out.println(path);
		Properties properties = null;
		File f = new File(path);
		if(!f.exists()){
			return properties;
		}
		InputStream in = null;
		FileInputStream fileInputStream = null;
		
		if(f.getName().endsWith(".properties") && !f.isDirectory()){
			try{
				properties = new Properties();
				fileInputStream = new FileInputStream(f.getAbsolutePath());
				in = new BufferedInputStream(fileInputStream);
				properties.load(in);
				
			}catch (Exception ex) {
				// TODO: handle exception
				ex.printStackTrace();
			}finally {
				try{
					if(in != null){
						in.close();
					}
					if(fileInputStream != null){
						fileInputStream.close();
					}
				}catch (Exception eee) {
					// TODO: handle exception
					eee.printStackTrace();
				}
			}
		}
		return properties;
	}
	/**
	 * TODO 发送socket消息
	 */
	public void sendSocketMsg() {
		String port=SpringInit.prop.get("listener.port")+"";
		String host=SpringInit.prop.get("listener.host")+"";
		String str[]=port.split(",");
		Timer timer=new Timer();
		try {
			timer.schedule(new TimerTask() {
				@Override
				public void run() {
					JSONArray jsarr=new JSONArray();
					for(int i=0;i<str.length;i++) {
						if(str[i]!=null&&str[i].length()>0) {
							JSONObject jsonobj=new JSONObject();
							jsonobj.put("port", Integer.valueOf(str[i]));
								try {
									jsonobj.put("status", PortUtils.bindPort(host, Integer.valueOf(str[i])));
								} catch (NumberFormatException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								} catch (Exception e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
							
							jsarr.add(jsonobj);
						}
					}
					myWebSocketHandler.broadcast(new TextMessage(jsarr.toString()));
				}
			},0,1000*5);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	

	
}
